package textapp.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import textapp.business.GroupService;
import textapp.connector.MessageCode;
import textapp.connector.ServerConnectorMessage;
import textapp.dao.model.Group;

/**
 * Servlet implementation class AddUserToGroupJsonServlet
 */
public class AddUserToGroupJsonServlet extends GenericJsonServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUserToGroupJsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	protected Object executeRequestJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try {

			int groupId = Integer.parseInt(request.getParameter("groupid"));
			String idStr = request.getParameter("ids");
			String[] idArr = idStr.split(";");
			ArrayList<Integer> idList = new ArrayList<Integer>();
			for (String i: idArr) {
				idList.add(Integer.parseInt(i));
			}
			if (idList.size() == 0) {
				throw new Exception();
			}
			Group group = new GroupService().addUsersToGroup(groupId, idList);
			return new ServerConnectorMessage(MessageCode.SUCCESS, group);
			
		} catch(Throwable t) {
			return new ServerConnectorMessage(MessageCode.FAIL);
		}
		
		
	}

    
	

}
